package com.wwd.movie.common.service;

import cn.hutool.json.JSONObject;
import cn.hutool.jwt.JWT;
import com.wwd.movie.login.domain.dto.LoginUser;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class TokenService {

    private final String secret = "mySecretKey123456"; // 建议放配置文件
    private final long expireTime = 60 * 60 * 1000 * 7 * 24; // 1小时

    public String createToken(LoginUser loginUser) {
        Date now = new Date();
        Date expireDate = new Date(now.getTime() + expireTime);

        return JWT.create()
                .setPayload("userId", loginUser.getUser().getId())
                .setPayload("username", loginUser.getUsername())
                .setIssuedAt(now)
                .setExpiresAt(expireDate)
                .setKey(secret.getBytes())
                .sign();
    }

    public JSONObject parseToken(String token) {
        return JWT.of(token).setKey(secret.getBytes()).getPayloads();
    }
}
